#!/usr/bin/env python

# --------------------------------------
# projects/python/primes/TestPrimes.py
# Copyright (C) 2009
# Austin Lawless
# --------------------------------------

# To run the tests
#     TestPrimes.py

# To document the tests
#     pydoc -w TestPrimes

import main
import unittest

# ----------
# TestReader
# ----------

class TestReader (object) :
    def __init__ (self, s) :
        self.s = s

    def read (self) :
        return self.s

# ----------
# TestWriter
# ----------

class TestWriter (object) :
    def str (self) :
        return self.s

    def write (self, *a) :
        self.s = str(a[0])
        for w in a[1:] :
            self.s += ' '
            self.s += str(w)
        self.s += '\n'

# -----------
# TestPrimes
# -----------

class TestPrimes (unittest.TestCase) :
    # ----
    # read
    # ----

    def test_read (self) :
        reader = TestReader('1\n')
        main.my_read(reader)
        self.assert_(main.x ==  1)

    # ----
    # eval
    # ----

    def test_eval1 (self) :
        main.x =  1
        main.my_eval()
        self.assert_(not main.successful)

    def test_eval2 (self) :
        main.x = 100
        main.my_eval()
        self.assert_(main.a == 2 and main.b == 2 and main.c == 43 and main.d == 53)

    def test_eval3 (self) :
        main.x = 9999999
        main.my_eval()
        self.assert_(main.a == 2 and main.b == 3 and main.c == 4999913 and main.d == 5000081)

    def test_eval4 (self) :
        main.x =  10000000
        main.my_eval()
        self.assert_(main.a == 2 and main.b == 2 and main.c == 4999783 and main.d == 5000213)
        

    # -----
    # print
    # -----

    def test_print (self) :
        main.x =  10000000
        writer = TestWriter()
        main.my_print(writer)
        self.assert_(writer.str() == '2 2 4999783 5000213\n')


if __name__ == "__main__" :
    unittest.main()
